﻿using System.Collections.ObjectModel;
using System.Linq;
using EzDesk.Core.Model;
using EzDesk.DesktopClient.AppModule.ViewContract;
using EzDesk.DesktopClient.Command;
using EzDesk.DesktopClient.Event;
using EzDesk.DesktopClient.MVVMEnhance;
using EzDesk.DesktopClient.ServiceLocator.CoreClientService;
using EzDesk.Generic.Collections;
using EzDesk.Generic;

namespace EzDesk.DesktopClient.AppModule.ViewModel
{
    public class ShortcutFunctionPanelViewModel : ViewModelWithSingleContainer<IShortcutFunctionPanelView>
    {
        public RangeCollection<AppFunctionViewModel> ShortcutAppFunctionViewModelCollection { get; set; }
        public DelegateCommand MoreFunctionCommand
        { get; set; }
        public ShortcutFunctionPanelViewModel()
        {
            ShortcutAppFunctionViewModelCollection = new RangeCollection<AppFunctionViewModel>(5);
            ShortcutAppFunctionViewModelCollection
                .AddRange(AppManagement.Instance
                .ShortCutFunctionCollection
                .Select(scfc => new AppFunctionViewModel(scfc)));

            MoreFunctionCommand = new DelegateCommand(this.OnMoreFunction);
            //AppFunctionSelectedEvent.Instance.Subscribe(this.OnAppFunctionSelected);
        }

        private void OnMoreFunction()
        {
            MoreFunctionEvent.Instance.Publish(new MoreFunctionArgs());
        }

        private void OnAppFunctionSelected(AppFunction appFunction)
        {
            var appFunctionViewModel =
                ShortcutAppFunctionViewModelCollection.FirstOrDefault(af => af.AppFunctionModel.Id == appFunction.Id);

            if (appFunctionViewModel != null)
            {
                ShortcutAppFunctionViewModelCollection.Remove(appFunctionViewModel);
                ShortcutAppFunctionViewModelCollection.Insert(0, appFunctionViewModel);
                return;
            }
            ShortcutAppFunctionViewModelCollection.Insert(0,  new AppFunctionViewModel(appFunction));

        }
    }
}